package Servlet;

import Damain.user;
import Service.UserService;
import Service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet(name = "UserLoginServlet", urlPatterns = "/userLoginServlet")
public class UserLoginServlet extends HttpServlet {
    private UserService Uservice = new UserServiceImpl();


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取前端传入的值
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        user login = Uservice.Login(username, password);
        //判断
        if (login == null) {
            //登录失败
            request.setAttribute("errorMsg", "用户名或者密码错误");
            request.getRequestDispatcher("/login.jsp").forward(request, response);
            return;
        } else {
            //登录成功
            request.setAttribute("user",login);
            response.sendRedirect("/pages/home.jsp");
            return;
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
